package com.kksoft.readcm.a;

import java.util.Calendar;
import java.util.Locale;

import android.os.AsyncTask;
import com.kksoft.readcm.objects.w;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import com.kksoft.readcm.objects.ErrorReport;
import com.kksoft.readcm.objects.GlobalVar;

final class LoginTask extends AsyncTask<Void, Void, Integer>  //old ab
{
  private String userID;
  private String passWord;
  private int loginType;
  private ErrorReport d = null;
  private LoginAppObservable e;

  
  public LoginTask(LoginAppObservable s1, String userid, String password, int logintype)
  {
      super();
      e = s1;
      d = null;
      this.userID = userid;
      this.passWord = password;
      switch(logintype){
      case 2:
    	  loginType = 6;
    	  break;
      default:
    	  loginType = 7;  
    	  break;
      }
  }

/* (non-Javadoc)
 * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
 */
@Override
protected void onPostExecute(Integer result) {
	// TODO Auto-generated method stub
	//Integer execType=(Integer)result;
	GlobalVar h1;
	if(result.intValue()==2)
	{
		h1 = new GlobalVar(24, null, true, this);
	}
	else
	{
		if(result.intValue() == -1)
		{
		     h1 = new GlobalVar(24, d.b(), false, this);
		}
		else
		{
		     h1 = new GlobalVar(24, d.b(), false, this);
		     
		}
	}
	e.h.a(h1);
}



@Override
protected Integer doInBackground(Void... params) {
	// TODO Auto-generated method stub
	//Object obj;
    Appconfig af = e.b;
    DefaultHttpClient defaulthttpclient = e.j.defaultHttpClient;
    w neww = new w(af, loginType, false, defaulthttpclient);
    neww.addParam(new BasicNameValuePair("username", this.userID));
    neww.addParam(new BasicNameValuePair("password", this.passWord));
    neww.addParam(new BasicNameValuePair("locale", Locale.getDefault().getLanguage()));
    neww.addParam(new BasicNameValuePair("conutry", Locale.getDefault().getCountry()));
    neww.addParam(new BasicNameValuePair("timezone", String.valueOf(String.valueOf(Calendar.getInstance().getTimeZone().getRawOffset() / 1000 / 60))));
    neww.httpRequest();
    int returncode;
    switch (neww.getLoginResult())
    {
    case 1:
    	returncode= Integer.valueOf(2);
    	break;
    case 2:
    	ErrorReport errorreport = neww.d();
        d = errorreport;
        returncode= Integer.valueOf(-1);
        break;
    case 3:
    	returncode= Integer.valueOf(-1);
    	break;
    default:
    	errorreport = neww.d();
        d = errorreport;
        returncode= Integer.valueOf(-1);
        break;
    }
    return returncode;
}
}